<?php
/**
 * 公共的函数
 * @author Yangdong Zhang <zhangyangdong@gouuse.cn>
 * @time 2018/10/12 16:41
 */

/**
 * 生成url地址
 * @param $param
 * @return string
 */
function url($param=[])
{
    if (getConfig('path_info')) {
        $site_url = str_replace('/index.php', '', SITE_URL);
        if (!$param) return $site_url;

        $mod = $param['mod'] ?? 'Index';
        $act = $param['act'] ?? 'index';
        if (isset($param['mod'])) unset($param['mod']);
        if (isset($param['act'])) unset($param['act']);

        if ($param) {
            return $site_url . '/' . $mod . '/' . $act . '?' . http_build_query($param);
        }else {
            return $site_url . '/' . $mod . '/' . $act;
        }
    }else {
        if (!$param) return SITE_URL;

        return SITE_URL . '?' . http_build_query($param);
    }
}


/**
 * 获取配置信息
 * @param $name
 * @return array
 */
function getConfig($name='')
{
    $config = include ROOT_PATH . '/config/app.php';
    if ($name) {
        return $config[$name] ?? null;
    }
    return $config;
}


if (!function_exists('getallheaders'))
{
    function getallheaders()
    {
        $headers = [];
        foreach ($_SERVER as $name => $value)
        {
            if (substr($name, 0, 5) == 'HTTP_')
            {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}